﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Common.Library;
using System.Text.RegularExpressions;
using TweetSharp;

namespace Social_Celebrities.Views
{
    public partial class Browser_Twitter : PhoneApplicationPage
    {
        string Parameter = string.Empty;
        string Link = string.Empty;
        TwitterLoginPage p = new TwitterLoginPage();
        public Browser_Twitter()
        {
            InitializeComponent();
            Loaded += Browser_Twitter_Loaded;
        }

        void Browser_Twitter_Loaded(object sender, RoutedEventArgs e)
        {
            var service = new TwitterService("LYTy3T8P556GxqRymimhwA", "5TSKipEjESOAcdBsViWm2E1RVxFEN1Ooh0nRwIj7RsY");
            if (p.getAccessToken() == null)
            {
                NavigationService.Navigate(new Uri("/Views/TwitterLoginPage.xaml", UriKind.Relative));
            }
            else
            {
                MyBrowser_Twittr.Navigate(new Uri("https://mobile.twitter.com/search?q=" + Parameter, UriKind.RelativeOrAbsolute));
            }
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if(NavigationContext.QueryString.TryGetValue("Parameter",out Parameter))
            {
            }
        }

        private void MyBrowser_Twittr_LoadCompleted(object sender, NavigationEventArgs e)
        {
            MessageBoxResult result = default(MessageBoxResult);
            try
            {
                Addrbar.Text = e.Uri.AbsoluteUri;
                Link = Addrbar.Text;

                if(!Link.Contains("search"))
                {
                    result = MessageBox.Show("Confirm this Celebrity?", "", MessageBoxButton.OKCancel);
                    if (result == MessageBoxResult.OK)
                    {
                        var item = Regex.Split(Link, "/")[3].Replace("?p=s", "");
                        //string TSName = Link.Substring(Link.LastIndexOf("/") + 1).Replace("?p=s","");
                        Constants.TwitterScreenName = item;
                        NavigationService.GoBack();
                    }
                    else
                        MyBrowser_Twittr.Navigate(new Uri("https://mobile.twitter.com/search?q=" + Parameter, UriKind.RelativeOrAbsolute));
                }
              AppSettings.BackgroundAgenError = "MyBrowser_Twitter_LoadCompleted Method on Browser_Twitter.xaml.cs Completed Successfully";
            }
            catch (Exception ex)
            {
                //Exceptions.SendErrortoMail("Exception at MyBrowser_Twitter_LoadCompleted Method in Browser_Twitter.xaml.cs Page and the error is " + " " + ex.Message + " " +ex.StackTrace);
                Exceptions.SaveOrSendExceptions("Exception at MyBrowser_Twitter_LoadCompleted Method on Browser_Twitter.xaml.cs file", ex);
                AppSettings.BackgroundAgenError = ex.Message;
            }
        }
    }
}